package com.dld.coupon.activity;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.dld.android.net.Param;
import com.dld.android.util.ReflectionFactory;
import com.dld.coupon.activity.CityActivity.NotifyChangeObserver;
import com.dld.coupon.util.MapUtil;
import com.dld.protocol.image.ImageProtocol;
import com.dld.protocol.json.Protocol;
import com.json.entity.MyCoupon;

public class MyCouponActivity extends BaseActivity implements
		NotifyChangeObserver {

	private ImageView topimage;
	private ImageView lowimage;
	private TextView text;
	private final static int CALLBACK_COMPLETE = 01;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == CALLBACK_COMPLETE) {
				MyCoupon mc = (MyCoupon) msg.obj;
				bindViewData(mc);
			}
		}
	};

	private void bindViewData(MyCoupon myconpon) {

		// ImageProtocol imp = new ImageProtocol(this, myconpon.getImage1());
		// imp.startTrans(topimage);
		//
		// ImageProtocol imp2 = new ImageProtocol(this, myconpon.getImage2());
		// imp2.startTrans(lowimage);

		SpannableStringBuilder sb = new SpannableStringBuilder();
		sb.append("销卡地址\n");
		sb.append(myconpon.getAddress() + "\n");
		sb.append(("联系人:"));
		sb.append(("" + myconpon.getContact() + "\n"));
		sb.append("联系电话:");
		int s1 = sb.length();
		if (myconpon.getTel() != null) {
			sb.append(myconpon.getTel());
		}
		int s2 = sb.length();
		UnderlineSpan us = new UnderlineSpan();
		sb.setSpan(us, s1 - 1, s2, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		text.setText(sb);
	}

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.mydldcoupon);
		init();
		CityActivity.registObserver(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	private void init() {
		topimage = (ImageView) findViewById(R.id.top_image);
		lowimage = (ImageView) findViewById(R.id.below_image);
		text = (TextView) findViewById(R.id.text_content);
		downLoadNetData();
	}

	private void downLoadNetData() {
		Param param = new Param();
		String cityid = MapUtil.getCityId();
		param.append("r", "www/api/Card").append("userAgent", "dld_android")
				.append("areaCode", cityid);
		Protocol pro = new Protocol(param);
		pro.startTrans(new Protocol.OnJsonProtocolResult(String.class) {

			@Override
			public void onException(String paramString, Exception paramException) {
				paramException.printStackTrace();
			}

			@Override
			public void onResult(String paramString, Object paramObject) {
				String content = (String) paramObject;
				try {
					Element root = DocumentBuilderFactory
							.newInstance()
							.newDocumentBuilder()
							.parse(new ByteArrayInputStream(content.getBytes()))
							.getDocumentElement();
					MyCoupon mc = ReflectionFactory.create(root
							.getElementsByTagName("result").item(0),
							MyCoupon.class);
					handler.sendMessage(Message.obtain(handler,
							CALLBACK_COMPLETE, mc));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	protected void onDestroy() {
		super.onDestroy();
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		return false;
	}

	public void onChangeCity(String paramString1, String paramString2) {
		downLoadNetData();
	}

}
